package com.zmx.patientend.view.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.wang.adapters.interfaceabstract.OnItemClickListener;
import com.zmx.patientend.BaseActivity;
import com.zmx.patientend.R;
import com.zmx.patientend.adapter.DoctorClassificationAdapter;
import com.zmx.patientend.adapter.DoctorItemAdapter;
import com.zmx.patientend.fragment.HomeFragment;
import com.zmx.patientend.okhttp.OkHttp3ClientManager;
import com.zmx.patientend.pojo.DepartmentCategory;
import com.zmx.patientend.pojo.Doctor;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FamousDoctorHallActivity extends BaseActivity {


    private RecyclerView re_doctor;
    private DoctorItemAdapter doctorItemAdapter;
    private List<Doctor> doctors;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_famous_doctor_hall;
    }

    @Override
    protected void initViews() {

        setToolbar(R.id.tool_bar);
        doctorItemAdapter = new DoctorItemAdapter(this);
        re_doctor = findViewById(R.id.re_doctor);
        re_doctor.setLayoutManager(new LinearLayoutManager(this));
        re_doctor.setAdapter(doctorItemAdapter);
        doctors = new ArrayList<>();
        doctorItemAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            protected void onItemClick(View view, int listPosition) {

                String did = doctors.get(listPosition).getMember_id() + "";
                //目前Activity→目标Activity
                Intent intent = new Intent(mActivity, DoctorDetailsActivity.class);
                intent.putExtra("doctor_id", did);
                intent.putExtra("class_id", doctors.get(listPosition).getClass_id() + "");
                intent.putExtra("hosp_id", doctors.get(listPosition).getHosp_id() + "");
                startActivity(intent);

            }
        });
        getAllDoctors();
    }



    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {


                case 2:
                    doctors.clear();

                    Log.e("内容",""+msg.obj.toString());
                    JSONObject jsonObject = null;


                    try {
                        jsonObject = new JSONObject(msg.obj.toString());
                        JSONArray array = jsonObject.getJSONArray("data");

                        Gson g = new Gson();
                        for (int i = 0; i < array.length(); i++) {

                            JSONObject object = array.getJSONObject(i);
                            Doctor c = g.fromJson(object.toString(), Doctor.class);
                            doctors.add(c);

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    doctorItemAdapter.setListAndNotifyDataSetChanged(doctors);//就是设置list然后刷新
                    break;

            }

        }
    };

    public void getAllDoctors() {

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("cmd", "getAllDoctor");
        params.put("data[row]", 1 + "");
        params.put("data[number]", 10 + "");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/api/", params, handler, 2, 404);

    }

}
